function create_world(ctx, a, matrix_w, matrix_h) {
	
	// create empty grid for next world //
	var next = new Array(matrix_w);
	for (var x = 0; x < next.length; x++) next[x] = new Array(matrix_h);
	
	// each current world
	for (var x = 0; x < a.length; x++) {
		for (var y = 0; y < a[x].length; y++) {				
				
			var n = 0;
			var neighbor = 0;
				
			// drawing //
			ctx.fillStyle = a[x][y] == 1 ? "red" : "white";
			ctx.fillRect(x * 10, y * 10,10,10);						
				
			ctx.strokeStyle = "#EFEFEF";
			ctx.strokeRect(x * 10, y * 10,10,10);	
				
			// copy current world to 
			next[x][y] = a[x][y];
				
			// find neighbor of current cell
			for (var dx = x -1; dx <= x + 1; dx++) {
				for (var dy = y -1; dy <= y + 1; dy++) {
					if(typeof a[dx] !== "undefined" && typeof a[dx][dy] !== "undefined"  ){							
						if(!(x == dx && y == dy)) {							
							if(a[x][y] == 0 && a[dx][dy] == 1) n++;								
							if (a[x][y] == 1 && a[dx][dy] == 1) neighbor++;	
						}							
					}		
				}
			}
				
			if(n == 3 || (neighbor == 2 || neighbor == 3)) {next[x][y] = 1;	}				
			else if(neighbor < 2 || neighbor > 3) next[x][y] = 0;
				
		}
	}	

	setTimeout( function() { 	create_world(ctx,next,matrix_w,matrix_h);	} , 30);
	
}



function conway_init() {
	
	canvas = document.getElementById('conway_world');	
	ctx = canvas.getContext('2d');	
	
	matrix_width = Math.floor(canvas.width / 10);
	matrix_height = Math.floor(canvas.height / 10);

	make = new Array(matrix_width);
	for (var x = 0; x < make.length; x++) {
		make[x] = new Array(matrix_height);
		for (var y = 0; y < make[x].length; y++) make[x][y] = Math.round(Math.random());
	}	
	
	create_world(ctx,make,matrix_width,matrix_height);		
}
